<!-- searchRusult -->
<style lang="scss" scoped>
  @import '~assets/common/css/mixin.scss';
  .search-rusult-container {
    height: 100%;
    @include flexbox(space-between,
    center,
    column,
    nowrap);
    .my-header {
      background: #fff;
      width: 100%;
      text-align: center;
      padding:10px;
    }
    .content {
      width: 100%;
      overflow-y: scroll;
    }
  }
</style>
<template>
  <div class="search-rusult-container">
    <div class="my-header">
      header
    </div>
    <div class="content">
      <div class="" v-for=" i in 100 " :key="i">
        {{i}}
      </div>
    </div>
    <div class="my-header">
      header
    </div>
  </div>


</template>

<script>
  import BackHead from 'common/backHead';
  import {
    searchGoods
  } from '@/service/getData';
  import {
    GetQueryString,
    GetParamString,
    setScrollTop
  } from '@/utils/mixin';
  import {
    Lazyload,
    Indicator,
    Loadmore,
    Toast,
    Popup
  } from 'mint-ui';
  // let jsonData = {
  //    list: []
  // }
  export default {
    data() {
      return {
        proData: [],
        allProLoad: true,
        allLoaded: false,
        token: '',
        showloadmore: false,
        bottomText: "上拉加载更多...",
        distance: 30,
        searchRusultData: [],
        // defaultParams: {
        //    query: GetQueryString('query') || '',
        //    sbar: GetQueryString('sbar') || 0,
        //    p: GetQueryString('p') || 1,
        //    brand: GetQueryString('brand') || '',
        //    style: GetQueryString('style') || '',
        //    sex: GetQueryString('sex') || '',
        //    y: GetQueryString('y') || '',
        //    s: GetQueryString('s') || '',
        //    price: GetQueryString('price') || '',
        //    num: GetQueryString('num') || '',
        //    stock: GetQueryString('stock') || 1,
        //    type: GetQueryString('type') || 1,
        //    img: GetQueryString('img') || 1,
        //    series: GetQueryString('series') || '',
        //    crowd: GetQueryString('crowd') || '',
        //    firstStyle: GetQueryString('firstStyle') || ''
        // },
        searchParams: {},
        isFirstEnter: false,
        defaultParams: {
          query: '',
          sbar: 0,
          p: 1,
          brand: '',
          style: '',
          sex: '',
          y: '',
          s: '',
          price: '',
          num: '',
          stock: 1,
          type: 1,
          img: 1,
          series: '',
          crowd: '',
          firstStyle: '',
          mixStock: 20
        },
        // newParams: {
        //    query: GetQueryString('query'),
        //    sbar: GetQueryString('sbar'),
        //    p: GetQueryString('p'),
        //    brand: GetQueryString('brand'),
        //    style: GetQueryString('style'),
        //    sex: GetQueryString('sex'),
        //    y: GetQueryString('y'),
        //    s: GetQueryString('s'),
        //    price: GetQueryString('price'),
        //    num: GetQueryString('num'),
        //    stock: GetQueryString('stock'),
        //    type: GetQueryString('type'),
        //    img: GetQueryString('img'),
        //    series: GetQueryString('series'),
        //    crowd: GetQueryString('crowd'),
        //    firstStyle: GetQueryString('firstStyle')
        // },
        jsonData: {
          list: []
        },
        newParams: {},
        pages: null,
        visiblePopup: {
          filters: false
        },
        firstFetch: true,
        list: [], //主数据
        isShowOuter: false, //是否显示 “不限” 页面
        outerList: [], // “不限” 页面里的数据
        outerQuery: "", //首字母筛选条件
        outerLink: {},
        outerName: "",
        commodityNum: 0, //筛选出的商品条数

      };
    },

    watch: {

    },
    components: {
      BackHead
    },
    computed: {

    },

    methods: {

      loadTop() {
        // 下拉刷新
        this.$refs.loadmore.onTopLoaded();
        this.searchParams.p = 1;
        this.fetchData();
      },
      loadBottom() {
        //底部加载更多
        this.$refs.loadmore.onBottomLoaded();
        this.$store.dispatch('SearchGoods', this.searchParams)
          .then(response => {
            this.proData = this.proData.concat(response.data.queryResult.itemList);
            //总页数
            this.pages = response.data.queryResult.pages;
            if (this.searchParams.p < this.pages) {
              this.searchParams.p += 1;
            } else {
              this.bottomText = '没有更多了';
              this.allLoaded = true;
            }
          });
      },
      filterData() {
        var ret = {};
        /*循环父*/
        this.list.map(item => {
          // /*循环子*/
          item.inner.map(innerItem => {
            if (innerItem.check) {
              ret[item.id] = innerItem.id;
            }
          })
        });
        //合并之后赋值到新的对象，不会污染this.defaultParams
        this.searchParams = JSON.parse(JSON.stringify(Object.assign({}, this.defaultParams, ret)));
        this.searchParams.p = 1;
        this.visiblePopup.filters = false;
        //设置到顶部
        setScrollTop(0);
        this.fetchData();
      },
      async fetchData(params) {

        let _this = this;
        //请求数据
        this.$store.dispatch('SearchGoods', this.searchParams)
          .then(response => {
            this.proData = response.data.queryResult.itemList;
            //重组筛选格式
            // if (_this.firstFetch) {
            //    this.formatFilter(response.data.resultList)
            //    _this.firstFetch = false;
            // }
            //总页数
            this.pages = response.data.queryResult.pages;
            this.showloadmore = true;
            if (this.searchParams.p < this.pages) {
              this.searchParams.p += 1;
            } else {
              this.bottomText = '没有更多了';
              this.allLoaded = true;
            }
          });
      },
      async initData() {
        let _this = this;
        //请求数据
        let response = await this.$store.dispatch('SearchGoods', this.defaultParams);
        let data = response.data.resultList;
        this.jsonData.list.push({
          name: "品牌",
          id: "brand",
          type: "radio",
          inner: data.brandList
        });
        this.jsonData.list.push({
          name: "品类",
          id: "firstStyle",
          type: "radio",
          inner: data.firstTypeList
        });
        this.jsonData.list.push({
          name: "上市时间",
          id: "y",
          type: "radio",
          inner: data.yearList
        });
        this.jsonData.list.push({
          name: "适穿季节",
          id: "s",
          type: "radio",
          inner: data.seasonList
        });
        this.jsonData.list.push({
          name: "适穿人群",
          id: "crowd",
          type: "radio",
          inner: data.crowdList
        });
        let newJsonData = JSON.parse(JSON.stringify(Object.assign({}, this.jsonData)));

        newJsonData.list.forEach(item => {
          item.show = false; //默认当直接子数据大于6条时，自动隐藏多余数据
          /*如果有“不限”， 给每个“不限”子数据添加check属性*/
          if (item.outer) {
            item.outer.list.forEach(function (outerItem, outerIndex, outer) {
              outer[outerIndex].check = false;
            })
          }
          item.inner.forEach(function (innerItem, innerIndex) {
            //如果品牌点击进来的
            if (innerItem.id == _this.$route.params.brand) {
              item.inner[innerIndex].check = true;
            } else {
              item.inner[innerIndex].check = false;
            }
          });
          _this.list.push(item);
        });

        // this.newParams = JSON.parse(JSON.stringify(Object.assign({}, this.defaultParams, this.$route.params)));
        //   console.log(this.newParams);
        // this.fetchData();
        //this.filterData = JSON.parse(JSON.stringify(Object.assign({}, data)))

      },
      // formatFilter(data) {
      //    var _this = this;
      //    jsonData.list.push({
      //       name: "品牌",
      //       id: "brand",
      //       type: "radio",
      //       inner: data.brandList
      //    });

      //    jsonData.list.push({
      //       name: "品类",
      //       id: "firstStyle",
      //       type: "radio",
      //       inner: data.firstTypeList
      //    });

      //    jsonData.list.push({
      //       name: "上市时间",
      //       id: "y",
      //       type: "radio",
      //       inner: data.yearList
      //    });

      //    jsonData.list.push({
      //       name: "适穿季节",
      //       id: "s",
      //       type: "radio",
      //       inner: data.seasonList
      //    })

      //    jsonData.list.push({
      //       name: "适穿人群",
      //       id: "crowd",
      //       type: "radio",
      //       inner: data.crowdList
      //    })
      //    let newJsonData = JSON.parse(JSON.stringify(jsonData));

      //    newJsonData.list.forEach(item => {
      //       item.show = false; //默认当直接子数据大于6条时，自动隐藏多余数据
      //       /*如果有“不限”， 给每个“不限”子数据添加check属性*/
      //       if (item.outer) {
      //          item.outer.list.forEach(function (outerItem, outerIndex, outer) {
      //             outer[outerIndex].check = false;
      //          })
      //       }
      //       item.inner.forEach(function (innerItem, innerIndex) {
      //          if (innerItem.name == "不限性别" || innerItem.name == "不限") {
      //             item.inner[innerIndex].check = true;
      //          } else {
      //             item.inner[innerIndex].check = false;
      //          }
      //       });
      //       _this.list.push(item);

      //    });
      //    console.log(_this.list) //this.filterData = JSON.parse(JSON.stringify(Object.assign({}, data)))

      // },
      resetFilter() {
        this.list.map(item => {
          item.inner.forEach(v => {
            v.check = false;
          })
        })
      },
      showResidue(item) {
        item.show = !item.show;
      },
      innerCheck(innerItem, item) {
        if (innerItem.check) {
          innerItem.check = false;
        } else {
          if (item.type != 'checkbox') {
            item.inner.forEach(function (v) {
              v.check = false;
            })
          }
          innerItem.check = true;
        }
      },
    },

    beforeRouteEnter(to, from, next) {
      if (from.name == 'index') {
        to.meta.isBack = true;
      }
      next();
    },
    activated: function () {

      if (this.$route.meta.isBack || this.isFirstEnter) {
        this.proData = [];
        this.searchParams = JSON.parse(JSON.stringify(Object.assign(this.defaultParams, this.$route.params)));
        this.fetchData();
      }
      this.$route.meta.isBack = false;
      this.isFirstEnter = false;
      //    this.proData = [];
      //    this.searchParams = JSON.parse(JSON.stringify(Object.assign(this.defaultParams, this.$route.params)));
      //    this.fetchData();
    },
    mounted: function () {
      this.initData();
      this.isFirstEnter = true;
      //this.searchParams = JSON.parse(JSON.stringify(Object.assign(this.defaultParams, this.$route.params)));
      //this.fetchData();
    },


  }
</script>